﻿using Api.Bll;
using Api.Model;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("_myAllowSpecificOrigins")]
    public class AdminController : ControllerBase
    {
        [HttpGet]
        public string GetLoginadmin(
            string name,
            string password)
        {
            
            try
            {
                Admin a=new Admin();
                a.name = name;
                a.password = password;

                AdminBll ab=new AdminBll();
                return JsonConvert.SerializeObject(ab.GetAdmin(a));
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            
        }
        [HttpPost]
        public string InsertAdmin(Admin admin)
        {
            AdminBll ab = new AdminBll();
            return ab.InsertAdmin(admin);
        }
        [HttpPut]
        public bool UpdateAdmin(Admin admin)
        {
            AdminBll ab = new AdminBll();
            return ab.UpdateAdmin(admin);
        }
        [HttpDelete]
        public bool DeleteAdmin(int uid)
        {
            AdminBll ab = new AdminBll();
            return ab.DeleteAdmin(uid);
        }

        //禁言用户
        [HttpPost("forbid")]
        public User forbid(int id)
        {
            AdminBll ab = new AdminBll();
            return ab.forbid(id);
        }

        //解除禁言
        [HttpPost("disforbid")]
        public User disForbid(int id)
        {
            AdminBll ab = new AdminBll();
            return ab.disForbid(id);
        }

        //审核帖子，返回null表示无未审核的帖子，否则，返回需要审核的帖子list
        [HttpGet("checkPost")]
        public List<Post> checkPost()
        {
            PostBLL pb = new PostBLL();
            List<Post> posts = pb.getAllUnchecked();
            if (posts.Count <= 0)
            {
                Console.WriteLine("无未审核的帖子。");
                return null;
            }
            return posts;


        }
        //审核帖子通过
        [HttpPost("pass")]
        public bool pass(int postId)
        {
            PostBLL pb = new PostBLL();
            return pb.pass(postId);
        }

        //查看所有用户
        [HttpPost("getAllUsers")]
        public List<User> getAllusers()
        {
            UserBLL ub = new UserBLL();
            return ub.getAllUsers();
        }

    }
}
